package org.springblade.modules.digitalization.manager;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.modules.digitalization.entity.A01Entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class LogManager {

	@Autowired
	private BladeLogger bladeLogger;


	public final static String TYPE_A01 = "A01";

	public final static String TYPE_ML = "ML";

	public final static String TYPE_WJ = "WJ";


	private final String OPT_ADD = "新增";
	private final String OPT_DELETE = "删除";
	private final String OPT_UPDATE = "修改";
	private final String OPT_IMPORT = "导入";


	public void doAdd(String id, String targetType, Object data) {
		bladeLogger.info(id + ":" + targetType + ":" + OPT_ADD, JsonUtil.toJson(data));
	}

	public void doDel(String id, String targetType, Object data) {
		bladeLogger.warn(id + ":" + targetType + ":" + OPT_DELETE, JsonUtil.toJson(data));
	}

	public void doUpdate(String id, String targetType, Object oldData, Object newData) {
		doBizUpdate(id, targetType, OPT_UPDATE, oldData, newData);
	}

	public void doBizUpdate(String id, String targetType, String opt, Object oldData, Object newData) {
		Map<String, Object> log = new HashMap<>();
		log.put("oldData", oldData);
		log.put("newData", newData);
		bladeLogger.info(id + ":" + targetType + ":" + opt, JsonUtil.toJson(log));
	}

	public void doImport(String id, String targetType, Object data) {
		bladeLogger.info(id + ":" + targetType + ":" + OPT_IMPORT, JsonUtil.toJson(data));
	}


	public void error(String id, String type, String opt, Object data, String message) {
		Map<String, Object> log = new HashMap<>();
		log.put("message", message);
		log.put("data", data);
		bladeLogger.error(id + ":" + type + ":" + opt, JsonUtil.toJson(log));
	}
}
